﻿using PLCS.Application.Contracts.TaskApp.Dtos;
using PLCS.Client.IService.OrderTask;

using System.Collections.ObjectModel;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.OrderTask;

public class TaskService : BaseService<TaskDto, TaskEditDto>, ITaskService
{
    public async Task AgainIssuedAsync(Guid Id)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"task/againIssued/{Id}");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task FinshTaskAsync(Guid Id)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"task/finshTask/{Id}");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task SendTaskAsync(TaskEditDto taskEditDto)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"task/sendTask", taskEditDto);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<PagedResultDto<TaskDto>> GetListBySearchAsync(int skipCount, int maxResultCount, string? taskId, string? taskType, string? taskStatus, string? trayNo)
    {
        var route = $"Task/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
        if (!string.IsNullOrEmpty(taskId))
            route += $"&taskNo={taskId}";
        if (!string.IsNullOrEmpty(taskType))
            route += $"&taskType={taskType}";
        if (!string.IsNullOrEmpty(taskStatus))
            route += $"&status={taskStatus}";
        if (!string.IsNullOrEmpty(trayNo))
            route += $"&trayNo={trayNo}";

        RequestParam requestParam = new(method: Method.Get, route: route);
        var apiRes = await restClient.ExecuteAsync<PagedResultDto<TaskDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new PagedResultDto<TaskDto>();
    }
}